package com.zkk.web;

import com.zkk.pojo.Student;
import com.zkk.service.StudentService;
import com.zkk.service.impl.StudentServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
@WebServlet("/add")
public class addServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            StudentService studentService = new StudentServiceImpl();
            Map<String, String[]> parameterMap = req.getParameterMap();  //只会获取你输入框的输入值，如果没有设置id的输入框，就不会取
            Student student = new Student();
            BeanUtils.populate(student,parameterMap);
            Student addStudent=studentService.add(student);
            req.getRequestDispatcher("/findAll").forward(req,resp);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
